home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 117 / PC Guia 117.iso / Software / Produtividade / Software2 / Product4 / Setup.exe / drupal-4.6.0 / includes / session.inc < prev    next >
Encoding:
Text File  |  2005-03-01  |  2.0 KB  |  73 lines

  1. <?php
  2. // $Id: session.inc,v 1.14 2005/03/01 20:15:10 dries Exp $
  3.  
  4. /**
  5.  * @file
  6.  * User session handling functions.
  7.  */
  8.  
  9. session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc");
  10. session_start();
  11.  
  12. /*** Session functions *****************************************************/
  13.  
  14. function sess_open($save_path, $session_name) {
  15.   return 1;
  16. }
  17.  
  18. function sess_close() {
  19.   return 1;
  20. }
  21.  
  22. function sess_read($key) {
  23.   global $user;
  24.  
  25.   $result = db_query_range("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = '%s' AND u.status < 3", $key, 0, 1);
  26.  
  27.   if (!db_num_rows($result)) {
  28.     db_query("INSERT INTO {sessions} (sid, uid, hostname, timestamp) VALUES ('%s', 0, '%s', %d)", $key, $_SERVER["REMOTE_ADDR"], time());
  29.     $result = db_query("SELECT u.* FROM {users} u WHERE u.uid = 0");
  30.   }
  31.  
  32.   $user = db_fetch_object($result);
  33.   $user = drupal_unpack($user);
  34.   $user->roles = array();
  35.  
  36.   $result = db_query("SELECT r.rid, r.name FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid = %d", $user->uid);
  37.  
  38.   while ($role = db_fetch_object($result)) {
  39.     $user->roles[$role->rid] = $role->name;
  40.   }
  41.  
  42.   return !empty($user->session) ? $user->session : '';
  43. }
  44.  
  45. function sess_write($key, $value) {
  46.   global $user;
  47.  
  48.   db_query("UPDATE {sessions} SET uid = %d, hostname = '%s', session = '%s', timestamp = %d WHERE sid = '%s'", $user->uid, $_SERVER["REMOTE_ADDR"], $value, time(), $key);
  49.  
  50.   return '';
  51. }
  52.  
  53. function sess_destroy($key) {
  54.   db_query("DELETE FROM {sessions} WHERE sid = '$key'");
  55. }
  56.  
  57. function sess_gc($lifetime) {
  58.  
  59.   /*
  60.   **  Be sure to adjust 'php_value session.gc_maxlifetime' to a large enough
  61.   **   value.  For example, if you want user sessions to stay in your database
  62.   **   for three weeks before deleting them, you need to set gc_maxlifetime
  63.   **   to '1814400'.  At that value, only after a user doesn't log in after
  64.   **   three weeks (1814400 seconds) will his/her session be removed.
  65.   */
  66.   db_query("DELETE FROM {sessions} WHERE timestamp < %d", time() - $lifetime);
  67.  
  68.   return 1;
  69.  
  70. }
  71.  
  72. ?>
  73.